package com.example.jobspringboot.config;

import com.example.jobspringboot.service.QuarzTask;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfiguration {

    @Bean(name = "jobDetail")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(QuarzTask quarzTask){
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        // 是否支持并发执行
        jobDetail.setConcurrent(false);

        jobDetail.setName("reptilianJob");

        jobDetail.setGroup("reptilianJobGroup");

        jobDetail.setTargetObject(quarzTask);

        // 需要执行的方法
        jobDetail.setTargetMethod("show");

        return jobDetail;
    }

    @Bean(name = "jobTrigger")
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetail jobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();

        tigger.setJobDetail(jobDetail);

        tigger.setCronExpression(" * * * * * ?");

        tigger.setName("Trigger");

        return tigger;
    }

    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactoryBean(Trigger jobTrigger){
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();

        factoryBean.setOverwriteExistingJobs(true);

        factoryBean.setStartupDelay(1);

        factoryBean.setTriggers(jobTrigger);

        return factoryBean;
    }
}
